package web.servlets;

import com.alibaba.fastjson.JSON;
import domain.Category;
import domain.Product;
import service.CatelogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.http.HttpClient;
import java.util.List;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: ShowProductServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2022/1/10 19:44
 * @version: 1.0
 */
public class ShowProductServlet extends HttpServlet {
    private static final String VIEW_CATEGORY="/WEB-INF/jsp/catalog/Category.jsp";

    private String categoryId;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        categoryId = req.getParameter("categoryId");
        CatelogService service = new CatelogService();
        Category category = service.getCategory(categoryId);
        List<Product> productList = service.getProductListByCategory(categoryId);

        resp.setContentType("text/json");
        PrintWriter pw = resp.getWriter();
        String msg = JSON.toJSONString(productList);
        System.out.println(msg);
        pw.print(msg);
        pw.flush();
        pw.close();
    }
}
